from django.core.management.base import BaseCommand
from products.models import ProductCategory, Product
from decimal import Decimal
from datetime import date

class Command(BaseCommand):
    help = '创建测试产品数据'
    
    def handle(self, *args, **options):
        # 获取产品分类
        try:
            stock_category = ProductCategory.objects.get(code='stock')
            bond_category = ProductCategory.objects.get(code='bond')
            fund_category = ProductCategory.objects.get(code='fund')
            cash_category = ProductCategory.objects.get(code='cash')
        except ProductCategory.DoesNotExist:
            self.stdout.write(
                self.style.ERROR('请先运行 init_product_categories 命令创建产品分类')
            )
            return
        
        # 测试产品数据
        test_products = [
            # 股票类产品
            {
                'code': 'SH000001',
                'name': '上证指数ETF',
                'category': stock_category,
                'product_type': 'growth',
                'risk_level': 4,
                'manager': '张三',
                'company': '华夏基金',
                'establishment_date': date(2020, 1, 1),
                'management_fee': Decimal('0.0050'),
                'custody_fee': Decimal('0.0025'),
                'subscription_fee': Decimal('0.0015'),
                'redemption_fee': Decimal('0.0005'),
                'min_investment': Decimal('1000'),
                'status': 'active'
            },
            {
                'code': 'SZ399001',
                'name': '深证成指ETF',
                'category': stock_category,
                'product_type': 'aggressive',
                'risk_level': 5,
                'manager': '李四',
                'company': '南方基金',
                'establishment_date': date(2019, 6, 1),
                'management_fee': Decimal('0.0060'),
                'custody_fee': Decimal('0.0025'),
                'subscription_fee': Decimal('0.0015'),
                'redemption_fee': Decimal('0.0005'),
                'min_investment': Decimal('1000'),
                'status': 'active'
            },
            # 债券类产品
            {
                'code': 'BOND001',
                'name': '国债ETF',
                'category': bond_category,
                'product_type': 'stable',
                'risk_level': 1,
                'manager': '王五',
                'company': '易方达基金',
                'establishment_date': date(2018, 3, 1),
                'management_fee': Decimal('0.0030'),
                'custody_fee': Decimal('0.0020'),
                'subscription_fee': Decimal('0.0008'),
                'redemption_fee': Decimal('0.0003'),
                'min_investment': Decimal('1000'),
                'status': 'active'
            },
            {
                'code': 'BOND002',
                'name': '企业债基金',
                'category': bond_category,
                'product_type': 'balanced',
                'risk_level': 2,
                'manager': '赵六',
                'company': '广发基金',
                'establishment_date': date(2017, 9, 1),
                'management_fee': Decimal('0.0040'),
                'custody_fee': Decimal('0.0020'),
                'subscription_fee': Decimal('0.0010'),
                'redemption_fee': Decimal('0.0005'),
                'min_investment': Decimal('1000'),
                'status': 'active'
            },
            # 基金类产品
            {
                'code': 'FUND001',
                'name': '混合型基金A',
                'category': fund_category,
                'product_type': 'balanced',
                'risk_level': 3,
                'manager': '孙七',
                'company': '嘉实基金',
                'establishment_date': date(2016, 12, 1),
                'management_fee': Decimal('0.0150'),
                'custody_fee': Decimal('0.0025'),
                'subscription_fee': Decimal('0.0150'),
                'redemption_fee': Decimal('0.0050'),
                'min_investment': Decimal('1000'),
                'status': 'active'
            },
            {
                'code': 'FUND002',
                'name': '平衡配置基金',
                'category': fund_category,
                'product_type': 'balanced',
                'risk_level': 3,
                'manager': '周八',
                'company': '博时基金',
                'establishment_date': date(2015, 5, 1),
                'management_fee': Decimal('0.0120'),
                'custody_fee': Decimal('0.0025'),
                'subscription_fee': Decimal('0.0120'),
                'redemption_fee': Decimal('0.0050'),
                'min_investment': Decimal('1000'),
                'status': 'active'
            },
            # 现金类产品
            {
                'code': 'CASH001',
                'name': '货币市场基金',
                'category': cash_category,
                'product_type': 'stable',
                'risk_level': 1,
                'manager': '吴九',
                'company': '天弘基金',
                'establishment_date': date(2014, 1, 1),
                'management_fee': Decimal('0.0030'),
                'custody_fee': Decimal('0.0010'),
                'subscription_fee': Decimal('0.0000'),
                'redemption_fee': Decimal('0.0000'),
                'min_investment': Decimal('100'),
                'status': 'active'
            }
        ]
        
        created_count = 0
        for product_data in test_products:
            product, created = Product.objects.get_or_create(
                code=product_data['code'],
                defaults=product_data
            )
            if created:
                created_count += 1
                self.stdout.write(
                    self.style.SUCCESS(f'创建测试产品: {product.name} ({product.code})')
                )
            else:
                self.stdout.write(
                    self.style.WARNING(f'测试产品已存在: {product.name} ({product.code})')
                )
        
        self.stdout.write(
            self.style.SUCCESS(f'测试产品创建完成，共创建 {created_count} 个新产品')
        )